package com.letian.web.controller.record;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.RecordPerson;
import com.letian.record.service.IRecordPersonService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 记录人际关系Controller
 *
 * @author bmz
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/record/person")
public class RecordPersonController extends BaseController {
    @Autowired
    private IRecordPersonService recordPersonService;

    /**
     * 查询记录人际关系列表
     */
    @PreAuthorize("@ss.hasPermi('record:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordPerson recordPerson) {
        startPage();
        List<RecordPerson> list = recordPersonService.selectRecordPersonList(recordPerson);
        return getDataTable(list);
    }

    /**
     * 查询记录人际关系列表
     */
    @PreAuthorize("@ss.hasPermi('record:person:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(RecordPerson recordPerson) {
        startPage();
        recordPerson.setCreateBy(getUsername());
        List<RecordPerson> list = recordPersonService.selectRecordPersonList(recordPerson);
        return getDataTable(list);
    }

    /**
     * 导出记录人际关系列表
     */
    @PreAuthorize("@ss.hasPermi('record:person:export')")
    @Log(title = "记录人际关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordPerson recordPerson) {
        List<RecordPerson> list = recordPersonService.selectRecordPersonList(recordPerson);
        ExcelUtil<RecordPerson> util = new ExcelUtil<RecordPerson>(RecordPerson.class);
        util.exportExcel(response, list, "记录人际关系数据");
    }

    /**
     * 获取记录人际关系详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") Long personId) {
        return success(recordPersonService.selectRecordPersonByPersonId(personId));
    }

    /**
     * 新增记录人际关系
     */
    @PreAuthorize("@ss.hasPermi('record:person:add')")
    @Log(title = "记录人际关系", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordPerson recordPerson) {
        recordPerson.setUserId(getUserId());
        recordPerson.setCreateBy(getUsername());
        return toAjax(recordPersonService.insertRecordPerson(recordPerson));
    }

    /**
     * 修改记录人际关系
     */
    @PreAuthorize("@ss.hasPermi('record:person:edit')")
    @Log(title = "记录人际关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordPerson recordPerson) {
        recordPerson.setUpdateBy(getUsername());
        Long count =recordPerson.getUpdateCount() == null ? 1 : recordPerson.getUpdateCount() + 1;
        recordPerson.setUpdateCount(count);
        return toAjax(recordPersonService.updateRecordPerson(recordPerson));
    }

    /**
     * 删除记录人际关系
     */
    @PreAuthorize("@ss.hasPermi('record:person:remove')")
    @Log(title = "记录人际关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable Long[] personIds) {
        return toAjax(recordPersonService.deleteRecordPersonByPersonIds(personIds));
    }
}
